package com.yinhai.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.lz.util.Utils;
import com.yinhai.activity.ContactActivity;
import com.yinhai.activity.R;
import com.yinhai.constant.Contact;
import com.yinhai.constant.UrlConstant;
import com.yinhai.model.UserUserContact;
import com.yinhai.util.NullUtils;

/**
 * 通讯录联系嗯分组列表
 * 
 * @author Administrator
 *
 */
public class ContactAdpter extends BaseAdapter {
    private LayoutInflater mInflater;
    
    private List<UserUserContact> smsList;
    
    private Context context;
    
    private boolean isMutiCheck;// 是否多选
    
    public ContactAdpter(Context context, List<UserUserContact> contacts) {
        mInflater = LayoutInflater.from(context);
        this.smsList = contacts;
        this.context = context;
    }
    
    public ContactAdpter(Context context, List<UserUserContact> contacts, boolean isMutilCheck) {
        this(context, contacts);
        this.isMutiCheck = isMutilCheck;
    }
    
    public ContactAdpter(Context context) {
        mInflater = LayoutInflater.from(context);
        this.smsList = new ArrayList<UserUserContact>();
        this.context = context;
    }
    
    public void assignment(List<UserUserContact> smsList) {
        this.smsList = smsList;
    }
    
    public void removeAll() {
        smsList.clear();
    }
    
    public void addCollection(List<UserUserContact> userContacts) {
        smsList.addAll(userContacts);
    }
    
    public void add(UserUserContact bean) {
        smsList.add(bean);
    }
    
    public void remove(int position) {
        smsList.remove(position);
    }
    
    public List<UserUserContact> getData() {
        return smsList;
    }
    
    @Override
    public int getCount() {
        return smsList.size();
    }
    
    @Override
    public Object getItem(int position) {
        return smsList.get(position);
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.contact_group_list_item, parent, false);
            holder = new ViewHolder();
            holder.contact_group_list_img = (ImageView)convertView.findViewById(R.id.contact_group_list_img);
            holder.contact_group_name = (TextView)convertView.findViewById(R.id.contact_group_name);
            holder.contact_group_num = (TextView)convertView.findViewById(R.id.contact_group_num);
            holder.contact_group_phone = (TextView)convertView.findViewById(R.id.contact_group_phone);
            holder.checkBox = (CheckBox)convertView.findViewById(R.id.item_check);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        AQuery aq = new AQuery(convertView);
        UserUserContact user = smsList.get(position);
        if (Contact.LOCAL_CONTACT_GROUP_ID.equals(user.getId())) {
            holder.contact_group_list_img.setImageDrawable(context.getResources()
                .getDrawable(R.drawable.local_contact_img));
        } else {
            if (user.getPhone() != null) {
                aq.id(R.id.contact_group_list_img).image(UrlConstant.LOCAL_URI + "getPhoto?phone=" + user.getPhone(),
                    Utils.getImageOptions(context, R.drawable.tel_photo, Utils.IsImgSize));
            } else {
                holder.contact_group_list_img.setImageDrawable(context.getResources().getDrawable(R.drawable.tel_group));
            }
            
        }
        if (Contact.MATCH_NAME.equals(user.getMatchType())) {
            SpannableStringBuilder style = new SpannableStringBuilder(user.getName());
            style.setSpan(new ForegroundColorSpan(Color.parseColor("#009900")),
                user.getStart(),
                user.getEnd(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.contact_group_name.setText(style);
        } else {
            holder.contact_group_name.setText(user.getName());
        }
        if (user.getPhone() != null) {
            if (Contact.MATHCH_TEL.equals(user.getMatchType())) {
                SpannableStringBuilder style = new SpannableStringBuilder(user.getPhone());
                style.setSpan(new ForegroundColorSpan(Color.parseColor("#009900")),
                    user.getStart(),
                    user.getEnd(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                holder.contact_group_num.setText(style);
            } else {
                holder.contact_group_num.setText(user.getPhone());
            }
            if (null == user.getDuty() || "null".equals(user.getDuty())) {
                holder.contact_group_phone.setText("");
            } else {
                holder.contact_group_phone.setText(user.getDuty());
            }
            
        } else {
            holder.contact_group_phone.setText("");
            if (NullUtils.isEmpty(smsList.get(position).getNum())) {
                holder.contact_group_num.setText("");
            } else {
                holder.contact_group_num.setText(smsList.get(position).getNum() + " 位联系人");
            }
        }
        holder.checkBox.setTag(user);
        if (isMutiCheck) {
            holder.checkBox.setVisibility(View.VISIBLE);
            holder.checkBox.setChecked(user.isCheck());
            
            holder.checkBox.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    UserUserContact userContact = (UserUserContact)arg0.getTag();
                    
                    if (context instanceof ContactActivity) {
                        ((ContactActivity)context).selectCheck(userContact);
                    }
                    
                }
            });
        } else {
            holder.checkBox.setVisibility(View.GONE);
        }
        convertView.setTag(holder);
        return convertView;
    }
    
    public final class ViewHolder {
        public TextView contact_group_name;
        
        public TextView contact_group_num;
        
        public TextView contact_group_phone;
        
        public ImageView contact_group_list_img;
        
        public CheckBox checkBox;
    }
    
}
